
(function() {
    //uuid生成方法
    // Private array of chars to use
    var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');

    Math.uuid = function (len, radix) {
        var chars = CHARS, uuid = [], i;
        radix = radix || chars.length;

        if (len) {
            // Compact form
            for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
        } else {
            // rfc4122, version 4 form
            var r;

            // rfc4122 requires these characters
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
            uuid[14] = '4';

            // Fill in random data.  At i==19 set the high bits of clock sequence as
            // per rfc4122, sec. 4.1.5
            for (i = 0; i < 36; i++) {
                if (!uuid[i]) {
                    r = 0 | Math.random()*16;
                    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                }
            }
        }

        return uuid.join('');
    };

    function Tongji(){
        this.init.call(this);
    }
    Tongji.prototype={
        init:function(){
            var _this=this;
            this.imgUrl = document.location.protocol + '//tj.huo.so/28490a8' ;
            this.obj={};
            /*if(typeof this.getCookie('tongji') != 'undefined'){
                var tongjiCookie=JSON.parse(_this.getCookie('tongji'));
                _this.obj.ui=tongjiCookie.uuid;
                _this.obj.nw=0
            }else{
                _this.obj.ui=Math.uuid(32);

                var cookieObj={isNew:true,uuid:_this.obj.ui};
                _this.setCookie("tongji",JSON.stringify(cookieObj));
                _this.obj.nw=1;
            }*/
            if(typeof this.getCookie('X-APIHitgou-ClientId') == 'undefined'){
                _this.obj.ui = '02' + Math.uuid(32);
                _this.setCookie("X-APIHitgou-ClientId",_this.obj.ui);
                _this.obj.nw=1;
            }else{
                _this.obj.ui = this.getCookie('X-APIHitgou-ClientId');
                _this.obj.nw = 0
            }
            if(typeof window.sessionStorage.tongji=='undefined'){
                _this.obj.url=_this.getUrl();
                _this.obj.at=_this.getTime();
                _this.obj.display=window.screen.width+"x"+window.screen.height;
                _this.obj.rnd=_this.getRnd(10);
                _this.obj.su=document.referrer;
                _this.obj.si=_this.getRnd(16);
                _this.obj.pk="5c43ycf3584gmf8w";//统计唯一标识，按项目划分不同
                //if(typeof mi=='undefined'){_this.obj.mi='ff014a0e5b27c179'}else{_this.obj.mi=mi};
                if(typeof mi=='undefined' || mi== ''){
                    if(typeof _this.getCookie("mid") != 'undefined'){
                        _this.obj.mi=_this.getCookie("mid");
                    }else{
                        _this.obj.mi=''; //默认为空，可以不传
                        _this.setCookie('mid','');
                    }
                }else{
                    _this.obj.mi=mi;
                    _this.setCookie('mid',mi);
                };
                _this.obj.od=0;
                window.sessionStorage.tongji=JSON.stringify(_this.obj);
            }else{
                var tongjiSession=JSON.parse(window.sessionStorage.tongji);
                _this.obj=tongjiSession;
                var now=_this.getTime();
                _this.obj.st=now-_this.obj.at;
                _this.obj.url=_this.getUrl();
                _this.obj.at=now;
                _this.obj.od=tongjiSession.od+1;
                _this.obj.su=document.referrer;
                _this.obj.nw=0;
                //console.log(_this.obj);
                window.sessionStorage.tongji=JSON.stringify(_this.obj);
            }
            //this.addImg(_this.obj);
        },
        getUrl:function(){
            return window.location.href;
        },
        addImg:function(obj){
            var img = new Image();
            var rnd_id = "_img_" + Math.random();
            window[rnd_id] = img; // 全局变量引用
            img.onload = img.onerror = function () {
                window[rnd_id] = null; // 删除全局变量引用
            };
            if(obj.od == 0){
                img.src = this.imgUrl+"/o.png?ds="+obj.display+"&rnd="+obj.rnd+"&ui="+obj.ui+"&si="+obj.si+"&pk="+obj.pk+"&mi="+obj.mi+"&at="+obj.at+"&od="+obj.od+"&nw="+obj.nw+"&su="+obj.su;
            }else{
                img.src = this.imgUrl+"/o.png?rnd="+obj.rnd+"&ui="+obj.ui+"&si="+obj.si+"&pk="+obj.pk+"&mi="+obj.mi+"&at="+obj.at+"&od="+obj.od+"&su="+obj.su+"&st="+obj.st;
            }
        },
        getTime:function(){//时间
            return Math.round(new Date().getTime()/1000);
        },
        getRnd:function(num){
            var max = Math.pow(10,num);
            var min = Math.pow(10,num-1);
            return parseInt(Math.random()*(max-min+1)+min);
        },
        setCookie : function(name,value){//写cookies
            var Days = 1000;
            var exp = new Date();
            exp.setTime(exp.getTime() + Days*24*60*60*1000);
            document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/;domain=.huo.so";
            //document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/";
        },
        getCookie : function(name){
            var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
            if(arr=document.cookie.match(reg))
                return unescape(arr[2]);
            else
                return undefined;
        },
        delCookie : function(name)
        {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval=this.getCookie(name);
            if(cval!=null)document.cookie= name + "="+cval+";expires="+exp.toGMTString()+";path=/;domain=.huo.so";
        },
    };
    var T=new Tongji();
    T.addImg(T.obj);
})();



